﻿using System;
using System.Data;
using System.Data.OleDb;
using System.IO;
using MySql.Data.MySqlClient;
using LdExpress;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
namespace LdExpress
{
    public class GoodPriceRouting
    {
        public long ID = -1;
        public string DC20 = "";
        public string HC40 = "";
        public string RF20 = "";
        public string RF40 = "";
        public string Routing = "";
        public long RoutingType = 1;//1 : Ha Phong, 2: Ho CHi Minh
        public string Description = "";
        public string Port = "";
        public long UserID = 0;
        public string CarrName = "";
        public DateTime LastUpdate = DateTime.Now;


        public GoodPriceRouting()
        {
        }

        public GoodPriceRouting(long lID)
        {
            this.ID = lID;
        }

        public void Save(MySqlConnection cn, MySqlTransaction trans)
        {
            string strSQL = "";
            string strTableName = "tbl_goodprice_routing";

            if (ID <= 0) // new object, we insert new record to database
            {
                //ID = long.Parse(DBModule.GetNewID(typeof(Bill), "tbl_bill" , "ID", "ID", cn, trans));
                strSQL = "INSERT INTO " + strTableName +
                        "(DC20," +
                        "HC40, " +
                        "RF20, " +
                        "RF40, " +
                        "Routing," +
                        "RoutingType," +
                        "Description, " +
                        "PORT, " +
                        "UserID, " +
                        "CarrName, " +
                        "LastUpdate" +
                        ")" +
                        "VALUES" +
                        "('" + DBModule.RefineString(DC20) + "'," +
                        "'" + DBModule.RefineString(HC40) + "'," +
                        "'" + DBModule.RefineString(RF20) + "'," +
                        "'" + DBModule.RefineString(RF40) + "'," +
                        "'" + DBModule.RefineString(Routing) + "'," +
                        RoutingType.ToString() + "," +
                        "'" + DBModule.RefineString(Description) + "'," +
                        "'" + DBModule.RefineString(Port) + "'," +
                        UserID.ToString() + "," +
                        "'" + DBModule.RefineString(CarrName) + "'," +
                        DBModule.RefineDatetime(LastUpdate) +
                        ")";

                // run SQL statement
                //throw new Exception(strSQL);
                DBModule.ExecuteNonQuery(strSQL, cn, trans);
                //GV.Response.Write(strSQL);
                UserObjectsLog.SaveObjectActionLog("goodprices", "add", cn, trans);                
            }
            else // edit object, we update old record in database
            {
                // build SQL statement
                strSQL = "Update " + strTableName + " set " +
                            "DC20='" + DBModule.RefineString(DC20) + "'" +
                            ",HC40='" + DBModule.RefineString(HC40) + "'" +
                            ",RF20='" + DBModule.RefineString(RF20) + "'" +
                            ",RF40='" + DBModule.RefineString(RF40) + "'" +
                            ",Routing = '" + DBModule.RefineString(Routing) + "'" +
                            ",RoutingType =" + RoutingType.ToString() +
                            ",Description ='" + DBModule.RefineString(Description) + "'" +
                            ",PORT = '" + DBModule.RefineString(Port) + "'" +
                            ",UserID = " + UserID.ToString() +
                            ",CarrName='" + DBModule.RefineString(CarrName) + "'" +
                            ",LastUpdate=" + DBModule.RefineDatetime(LastUpdate) + "" +
                    " where ID=" + ID.ToString();
                // run SQL statement
                DBModule.ExecuteNonQuery(strSQL, cn, trans);
                UserObjectsLog.SaveObjectActionLog("goodprices", "edit", cn, trans);                
            }
        }
        public void Delete(MySqlConnection cn, MySqlTransaction trans)
        {

            string strSQL = "DELETE FROM tbl_goodprice_routing WHERE ID = " + this.ID.ToString();
            DBModule.ExecuteNonQuery(strSQL, cn, trans);
            UserObjectsLog.SaveObjectActionLog("goodprices", "delete", cn, trans);
        }
        public void Load(MySqlConnection cn, MySqlTransaction trans)
        {
            string strSQL = "SELECT * FROM tbl_goodprice_routing WHERE ID = " + ID.ToString() + " limit 0,1";

            DataSet ds = DBModule.ExecuteQuery(strSQL, cn, trans);
            //Fill data into this object
            if (ds.Tables[0].Rows.Count > 0)
            {
                DataRow dr = ds.Tables[0].Rows[0];
                if (!dr.IsNull("ID")) ID = long.Parse(dr["ID"].ToString());
                if (!dr.IsNull("DC20")) DC20 = dr["DC20"].ToString();                
                if (!dr.IsNull("HC40")) HC40 = dr["HC40"].ToString();
                if (!dr.IsNull("RF20")) RF20 = dr["RF20"].ToString();
                if (!dr.IsNull("RF40")) RF40 = dr["RF40"].ToString();
                if (!dr.IsNull("Routing")) Routing = dr["Routing"].ToString();
                if (!dr.IsNull("RoutingType")) RoutingType = long.Parse(dr["RoutingType"].ToString());
                if (!dr.IsNull("Description")) Description = dr["Description"].ToString();
                if (!dr.IsNull("Port")) Port = dr["Port"].ToString();
                if (!dr.IsNull("UserID")) UserID = int.Parse(dr["UserID"].ToString());
                if (!dr.IsNull("CarrName")) CarrName = dr["CarrName"].ToString();
                if (!dr.IsNull("LastUpdate")) LastUpdate = DateTime.Parse(dr["LastUpdate"].ToString());
            }
        }

        public static void GetList(string Condition, string OrderBy, int NumOfItemsPerPage, int PageNo, out int NumberOfPages, out DataSet ds, MySqlConnection cn, MySqlTransaction trans)
        {
            NumberOfPages = 0;
            string strSQL = "";
            ds = null;
            // build SQL statement
            strSQL = "Select * from tbl_goodprice_routing where 1=1 ";
            if (Condition != "")
            {
                strSQL += Condition;
            }
            if ((OrderBy != null) && (OrderBy != ""))
                strSQL = strSQL + " Order By " + DBModule.RefineString(OrderBy);
            // run SQL statement
            if (NumOfItemsPerPage > 0)
            {
                ds = DBModule.ExecuteQuery(strSQL, NumOfItemsPerPage, PageNo, out NumberOfPages, cn, trans);
            }
            else
                ds = DBModule.ExecuteQuery(strSQL, cn, trans);

        }
        public void DoSearch(string strKeywords, string OrderBy, out DataSet ds, MySqlConnection cn, MySqlTransaction trans)
        {
            string strSQL = "";
            ds = null;
            // build SQL statement
            strSQL = "Select `ID` from tbl_goodprice_routing where 1=1 ";

            if ((OrderBy != null) && (OrderBy != ""))
                strSQL = strSQL + " Order By " + DBModule.RefineString(OrderBy);
            // run SQL statement
            ds = DBModule.ExecuteQuery(strSQL, cn, trans);
        }
    }
}
